using UnityEngine;
using System.Collections;

public class FatSkinnySwap : MonoBehaviour {
	public GameObject skinnyGuy;
	public GameObject fatGuy;
	//public Camera cam;
	
	private GameObject currentGuy;
	
	public bool areYouAFatty = false;
	
	public Vector3 lastKnownPosition = Vector3.zero;
	
	// Use this for initialization
	void Start () {
		lastKnownPosition = transform.position;
		areYouAFatty = false;
		//currentGuy = skinnyGuy;
		SwapBodies ();
		 Messenger.AddListener("Ate food", OnAteFood );
		 Messenger.AddListener("Threw up", OnThrewUp );
	}

	void OnAteFood()
	{
		SwapBodies();
	}

	void OnThrewUp()
	{
		SwapBodies();
	}
	
	void SwapBodies() {
		if (areYouAFatty) {
			Debug.Log("Becoming skinny.");
			DestroyObject (currentGuy);
			currentGuy = (GameObject) Instantiate (skinnyGuy, lastKnownPosition + new Vector3(0,5,0), Quaternion.identity);
			areYouAFatty = false;
		}
		else
		{
			Debug.Log("Becoming fat.");
			DestroyObject (currentGuy);
			currentGuy = (GameObject) Instantiate (fatGuy, lastKnownPosition, Quaternion.identity);
			areYouAFatty = true;

		//cam.transform.parent = currentGuy.transform;

		}
	}
	
	// Update is called once per frame
	void Update () {
		
//		if (Input.GetButtonUp ("Fire1")) {
//			if (areYouAFatty) areYouAFatty = false;
//			else
//				areYouAFatty = true;
//			
//			SwapBodies();
//		}

		transform.position = currentGuy.transform.position;
		if (currentGuy != null) lastKnownPosition = currentGuy.transform.position;
			else lastKnownPosition = transform.position; 
	}
}
